package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.ShoppingCartGoodsModel;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/3/7.
 */

public class ShoppingHolder extends RecyclerView.ViewHolder implements ShoppingCartGoodsListAdapter.OnTotalChangedListener {
    @Bind(R.id.layout_select)
    View selectLayout;
    @Bind(R.id.select)
    CheckBox select;
    @Bind(R.id.shop)
    TextView shop;
    @Bind(R.id.edit)
    TextView edit;
    @Bind(R.id.complete)
    TextView complete;
    @Bind(R.id.goods_list)
    RecyclerView goodsList;

    private ShoppingCartGoodsListAdapter adapter;

    public ShoppingHolder(View itemView) {
        super(itemView);

        ButterKnife.bind(this, itemView);
    }

    public void setAdapter(Context context, List<ShoppingCartGoodsModel> list, boolean mode) {
        if (adapter == null) {
            adapter = new ShoppingCartGoodsListAdapter(list, mode);
            goodsList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
            goodsList.setAdapter(adapter);
            adapter.setOnTotalChangedListener(this);
        } else {
            adapter.refresh(list, mode);
        }
    }

    public void selectAll() {
        adapter.selectAll();
    }

    public void unSelectAll() {
        adapter.unSelectAll();
    }

    void changeMode(boolean mode) {
        adapter.changeMode(mode);
    }

    @Override
    public void onTotalChanged(float total, int shopId, boolean selectAll) {
        select.setChecked(selectAll);
        if (listener != null) {
            listener.onStateChanged(total, shopId);
        }
    }

    @Override
    public void editGoods(ShoppingCartGoodsModel model) {
        if (listener != null) {
            listener.editGoods(model);
        }
    }

    private OnShopStateChangedListener listener;

    public interface OnShopStateChangedListener {
        void onStateChanged(float coast, int shopId);
        void editGoods(ShoppingCartGoodsModel model);
    }

    public void setOnShopCoastChangedListener(OnShopStateChangedListener listener) {
        this.listener = listener;
    }
}
